﻿using System;
using System.IO;
using UnityEngine;

namespace ZXthex
{
    [Serializable]
    public class ConsoleObject : ScriptableObject
    {
        enum WhenStartEnter { 启动, 不启动 }

        enum WhenPlayOver { 关闭, 保持开启 }

        enum CleanInput {清空输入缓存__实验功能__, 不清理 }

        enum CleanOutput { 添加分隔标记, 清空控制台, 不清理  }

        public enum CleanStyle { MarkLine, Clean, DoNothing }

        [SerializeField]
        private WhenStartEnter m_开始运行时控制台行为 = WhenStartEnter.启动;

        [SerializeField]
        private WhenPlayOver m_运行结束时控制台行为 = WhenPlayOver.保持开启;

        [SerializeField]
        private CleanInput m_开始运行时清理输入行为 = CleanInput.不清理;

        [SerializeField]
        private CleanOutput m_开始运行时清理控制台行为 = CleanOutput.添加分隔标记;
        
        [HideInInspector]
        [SerializeField]
        private bool m_stateShouldOpen; //用于记录当前控制台是否应该是开启状态，用来防止显示的控制台是Detach的

        public bool stateShouldOpen
        {
            get
            {
                return m_stateShouldOpen;
            }
            set
            {
                m_stateShouldOpen = value;
                Save();
            }
        }

        /// <summary>
        /// 在开始运行时，自动启动控制台
        /// </summary>
        public bool startOnStartPlay
        {
            get { return m_开始运行时控制台行为 == WhenStartEnter.启动; }
            set { m_开始运行时控制台行为 = value ? WhenStartEnter.启动 : WhenStartEnter.不启动; Save(); }
        }

        /// <summary>
        /// 在开始运行时，是否清空input buffer
        /// </summary>
        public bool cleanInputOnStartPlay
        {
            get { return m_开始运行时清理输入行为 == CleanInput.清空输入缓存__实验功能__; }
            set { m_开始运行时清理输入行为 = value ? CleanInput.清空输入缓存__实验功能__ : CleanInput.不清理; Save(); }
        }

        /// <summary>
        /// 在开始运行时，是否清空控制台显示
        /// </summary>
        public CleanStyle cleanOutputOnStartPlay
        {
            get { return (CleanStyle)m_开始运行时清理控制台行为 ; }
            set { m_开始运行时清理控制台行为 = (CleanOutput)value; Save(); }
        }

        /// <summary>
        /// 当运行结束时，保持开启控制台
        /// </summary>
        public bool liveOnEndPlay
        {
            get { return m_运行结束时控制台行为 == WhenPlayOver.保持开启; }
            set { m_运行结束时控制台行为 = value ? WhenPlayOver.保持开启 : WhenPlayOver.关闭; Save(); }
        }


        const string foldName = "Console Setting";
        const string fileName = "System Console Config.asset";


        static string path;


        public static ConsoleObject GetConsoleObject()
        {
            var strs = UnityEditor.AssetDatabase.FindAssets(
                            nameof(UnityCommandLineSetting));
            if (path == null)
            {
                for (int i = 0; i < strs.Length; i++)
                {
                    path = UnityEditor.AssetDatabase.GUIDToAssetPath(strs[i]);
                    var script = UnityEditor.AssetDatabase.
                                            LoadAssetAtPath<UnityEditor.MonoScript>(path);
                    if (script != null && script.GetClass() == typeof(UnityCommandLineSetting))
                    {
                        path = Path.GetDirectoryName(path);
                        break;
                    }
                }

                if (path != null)
                {
                    string completePath = Path.Combine(Path.GetFullPath(path), foldName);
                    if (!Directory.Exists(completePath))
                    {
                        UnityEditor.AssetDatabase.CreateFolder(path, foldName);
                    }

                    path = Path.Combine(path, foldName, fileName);

                    var ret = UnityEditor.AssetDatabase.LoadAssetAtPath
                                                                <ConsoleObject>(path);
                    if (ret != null) return ret;

                    UnityEditor.AssetDatabase.CreateAsset
                                                 (CreateInstance<ConsoleObject>(), path);

                    UnityEditor.AssetDatabase.SaveAssets();

                    return UnityEditor.AssetDatabase.LoadAssetAtPath<ConsoleObject>(path);

                }
            }
            else
            {
                return UnityEditor.AssetDatabase.LoadAssetAtPath<ConsoleObject>(path);
            }

            Debug.LogError("找不到设置文件夹。。");
            return null;
        }

        void Save()
        {
            UnityEditor.EditorUtility.SetDirty(this);
            UnityEditor.AssetDatabase.SaveAssets();
        }

    }
}